package com.niiwoo.civet.trade.service.local.credit;

import com.niiwoo.civet.trade.dao.entity.CreditorSaleDetail;
import com.niiwoo.civet.trade.dao.entity.CreditorSelling;
import com.niiwoo.civet.trade.dao.mapper.CreditorSaleDetailMapperExt;
import com.niiwoo.civet.trade.dao.mapper.CreditorSellingMapperExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

@Service
public class CreditorSellingLocalService {
    @Autowired
    private CreditorSaleDetailMapperExt creditorSaleDetailMapperExt;

    @Autowired
    private CreditorSellingMapperExt creditorSellingMapper;

    @Transactional
    public CreditorSaleDetail getCreditorSaleDetail(Long creditorSaleDetailId, String saleUserId) {
        CreditorSaleDetail creditorSaleDetail = creditorSaleDetailMapperExt.selectByPrimaryKeyAndSaleUserId(creditorSaleDetailId, saleUserId);
        CreditorSelling selling = creditorSellingMapper.selectByPrimaryKey(creditorSaleDetailId);
        if (Objects.nonNull(creditorSaleDetail)) {
            if(Objects.isNull(selling)){
                creditorSaleDetail.setStatus((byte)3);
            }else{
                creditorSaleDetail.setStatus(selling.getStatus() == ((byte) 1) ? (byte) 1 : (byte) 2);
            }
        }
        return creditorSaleDetail;
    }


}
